<?php
/*
 *  This file is part of Urd.
 *
 *  Urd is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *  Urd is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program. See the file "COPYING". If it does not
 *  exist, see <http://www.gnu.org/licenses/>.
 *
 * $LastChangedDate: 2008-07-01 22:12:05 +0200 (Tue, 01 Jul 2008) $
 * $Rev: 1251 $
 * $Author: styck $
 * $Id: install.php 1251 2008-07-01 20:12:05Z styck $
 */
define('ORIGINAL_PAGE', $_SERVER['PHP_SELF']);

// Suppress warning messages:
error_reporting(E_ERROR);

// Store variables in $_SESSION so they're remembered:
$pathca = realpath($pathlo . '/../functions/');
session_name('URD_WEB' . md5($pathca)); // add the hashed path so we can have more than 1 session to different urds in one browser
@session_start();

// Installer script:

// If .installed exists, we're already installed.
if (file_exists('../.installed')) die("URD is already installed. Remove the file '.installed' to enable the install script.");

$maxpages = 6;
$page = 0;
if (isset($_REQUEST['page']))
	$page = $_REQUEST['page'];

$OUT = <<<CT
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>URD Installation script</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="resource-type" content="document"/>
<link rel="SHORTCUT ICON" href="../html/favicon.ico" type="image/x-icon"/>
<!--link rel="stylesheet" href="../html/smarty/templates/default/style.css" type="text/css"/-->
<script language="javascript">

function LoadPage(pagenumber)
{
	var installform = document.forms[0];
	var pagenr = document.getElementById('page');
	pagenr.value = pagenumber;
	installform.submit();
}

</script>
<style>
html
{
	border: none;
	background-image: url('../html/smarty/templates/default/img/background.png');
}
body
{
	background-image: url('../html/smarty/templates/default/img/background.png');
	color: white;
	margin-left: 50px;
	padding: 0px;
	font-family: Geneva, Arial, Helvetica, sans-serif;
        font-size: small;
}
#install
{
	width: 50%;
	color: white;
}
td.install_OK
{
	font-size: large;
	font-weight: bold;
	color: green;
	text-align: center;
}
td.install_Failed
{
	font-size: large;
	font-weight: bold;
	color: red;
	text-align: center;
}
td.install1
{
	font-weight: bold;
}
td.install2
{
	width: 80%;
	text-align: left;
}
a
{
	color: #cccccc;
	text-decoration: none;
}
table.installation
{
	width: 100%;
}
.noborder
{
	border-style: none;
}
img
{
	border-style: none;
}
.info
{
	font-style: oblique;
	font-size: x-small;
	color: yellow;
}
#installation
{
	overflow: scroll;
	overflow-x: auto;
	overflow-y: auto;
}

.buttonlike
{
	cursor: pointer;
}

</style>
</head>
<body>
<div id="headerleft">
    <img src="../html/smarty/templates/default/img/urdlogo.png" alt="Usenet Resource Downloader" class="urdlogo" title="URD"/>
</div>

<div id="seperator"></div>

<div id="left">
&nbsp;
</div>

<div id="installation">
<h2>Installation script ($page/$maxpages)</h2>
<form method=post name="installform" id="installform">
<input type="hidden" name="page" id="page" value="">
<table class="installation">
CT;


$continuepic = '<img src="../html/smarty/templates/default/img/forward.png" alt="" title="Continue" class="buttonlike"/>';
$refreshpic = '<img src="../html/smarty/templates/default/img/reload.png" alt="" title="Reload" class="buttonlike"/>';

require_once('install.funcs.inc.php');
// An actual installation page or the frontpage?:
if ($page == 0) {
	$OUT .= <<<BDY
		<tr><td colspan=2>
		This is the installation script. <br /> It will check if PHP is configured
		correctly, if all required applications are available, if optional applications are available,
		and will ultimately help you in getting URD up and running.
		<br /><br />
		</td></tr>
BDY;
	// Ok, we're about to install, make sure a 'config.php' file exists or all includes will throw a fatal error because require fails:
	$OUT .= '<tr><td colspan="2" class="install1">Pre-install check:</td></tr>' . "\n";

	$OUT .= '<tr><td class="install2">config.php can be written</td>';
	$OUT .= GenRetVal(touch('../config.php'), $rv1);

	// And also check the smarty dir:
	$OUT .= '<tr><td class="install2">Smarty cache directory can be written to</td>';
	$OUT .= GenRetVal(touch('../html/smarty/c_templates/default/installcheck'), $rv2);
	if (!$rv1) {
		$OUT .= ShowHelp("Error! Config.php could not be written, please make sure that the file/directory permissions are correct!");
	}
	if (!$rv2) {
		$OUT .= ShowHelp("Error! /html/smarty/c_templates/default/ could not be written to, please make sure that the directory permissions are correct!");
	}

	if (!$rv1 OR !$rv2) {
		$OUT .= ShowHelp("The installer will not work until this has been fixed. You can refresh to check again.");
		$OUT .= '<tr><td><a href="install.php" class="noborder" title="Refresh">'.$refreshpic.'</a></td></tr>';
	} else {
		$OUT .= '<tr><td colspan=2><br/><a onclick="LoadPage(1);" class="buttonlike">Start Installation Wizard</a></td></tr>';
	}

} else {

	if (!is_numeric($page)) die("What are you trying to pull?");


	// Catch exceptions (installation errors) by stopping
	try {
		require_once('../functions/functions.php');
		include('install.'.$page.'.inc.php');
	} catch (exception $e) {
		$OUT .= "Oops, uncaught exception! : " . $e->getMessage();
	}
}

$OUT .= <<<LAST
</table>
</form>
</div>

</body>
</html>
LAST;

echo $OUT;

die(); // Nothing should come after here anyways.
?>
